package GameMain.View.PTGame
{
	import Engine.Module.ModuleBase;
	
	import GameMain.Game;
	import GameMain.Macro.MacroEvent;
	import GameMain.Model.Entity;
	import GameMain.Model.EntityNPC;
	import GameMain.Model.EntitySkill;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	import library.SocketData.SocketData;
	
	public class PVPGame extends ModuleBase
	{
		
		private var _cadenceCtrl:CadenceCtrl = null;
		private var _mapArea:MapArea = null;
		private var _losWinSprite:MovieClip = null;
		
		public function PVPGame(p_content:Sprite=null)
		{
			super(p_content);
			this.initUI();
		}
		
		private function initUI():void
		{
			this.graphics.lineStyle(1);
			this.graphics.beginFill(0x000000,1.0);
			this.graphics.drawRect(0, 0, 1000,600);
			this.graphics.endFill();
			
			var _flashBg:Sprite = new Sprite();
			_flashBg.graphics.lineStyle(5,0xffffff);
			_flashBg.graphics.drawRect(0, 0, 973,483);
			_flashBg.graphics.endFill();
			_flashBg.x = 10;_flashBg.y = 10;
			
			this._mapArea = new MapArea();
			this.addChild(this._mapArea);
			
			var gameBack:Sprite = new GameManager.ClassGameBack() as Sprite;
			this.addChild(gameBack);
			gameBack.x = 14; gameBack.y = 503;
			this._cadenceCtrl = new CadenceCtrl(this,gameBack);
		}
		
		public function initGameFromData(mapID:int):void
		{
			this._mapArea.setMap(GameManager.getMapDataFromID(mapID));
			this._mapArea.addAllPlayer();
		}
		
		public function setNewPlayer(b:Boolean):void
		{
			this._cadenceCtrl.booleanNewPlayer = b;
		}
		
		
		public function resetGame():void
		{
			this._mapArea.clearMap();
		}
		
		public function regesterEvent():void
		{
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_POSITION,onSetEntityPosition);
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_FACE,onSetEntityFace);
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_NPC,onCreateNPC);
		}
		
		private function onSetEntityAction(data:SocketData):void
		{
			var entityID:int = data.readInt16();
			var actionState:int = data.readInt8();
			(Game.EntityMgr.getEntity(entityID) as Entity).playAction(actionState);
		}
		
		private function onSetEntityPosition(data:SocketData):void
		{
			while(data.position != data.length)
			{
				var entityID:int = data.readInt16();
				var _x:int = data.readInt16();
				var _y:int = data.readInt16();
				Game.EntityMgr.getEntity(entityID).setPosition(_x,_y);
			}
		}
		
		private function onSetEntityFace(data:SocketData):void
		{
			var entityID:int = data.readInt16();
			var face:int = data.readInt8();
			Game.EntityMgr.getEntity(entityID).setFaceDirection(face);
		}
		
		private function onEntityMove(data:SocketData):void
		{
			var entityID:int = data.readInt16();
			Game.EntityMgr.getEntity(entityID).onEntityMove(data);
		}
		
		private function onEntityStop(data:SocketData):void
		{
			var entityID:int = data.readInt16();
			Game.EntityMgr.getEntity(entityID).onEntityStop(data);
		}
		
		private function onSetPlayerHP(data:SocketData):void
		{
			var entityID:int = data.readInt16();
			(Game.EntityMgr.getEntity(entityID) as Entity).setPlayerHP(data.readInt16());
		}
		
		private function onCreateSkill(data:SocketData):void
		{
			var skill:EntitySkill = new EntitySkill();
			skill.setEntityID(data.readInt16());
			skill.setSkillID(data.readInt32());
			skill.setPosition(data.readInt16(),data.readInt16());
			skill.setFaceDirection(data.readInt8());
			skill.setVisible(Boolean(data.readInt8()));
			Game.EntityMgr.insertEntity(skill);
			skill.addToParent(this._mapArea.getPlayerGroud());
		}
		
		private function onCreateNPC(data:SocketData):void
		{
			var npc:EntityNPC = new EntityNPC();
			npc.setEntityID(data.readInt16());
			npc.setNPCid(data.readInt32());
			npc.setPosition(data.readInt16(),data.readInt16());
			npc.setFaceDirection(data.readInt8());
			Game.EntityMgr.insertEntity(npc);
			npc.addToParent(this._mapArea.getNpcGroud());
		}
		
		public function setLoseWinSprite(c:Class):void
		{
			this._losWinSprite = new c as MovieClip;
		}
		
		private function onEntityRemove(data:SocketData):void
		{
			Game.EntityMgr.removeEntity(data.readInt16());
		}
		
		private function onGameOver(data:SocketData):void
		{
			var state:int = data.readInt8();
			this._losWinSprite.gotoAndStop(state+1);
			this._losWinSprite.visible = true;
			this._losWinSprite.x = 429;
			this._losWinSprite.y = 110;
			this.addChild(this._losWinSprite);
			
		}
		
		override protected function onShowBack():void
		{
			this._cadenceCtrl.regestEvent();
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_ACTION,onSetEntityAction);
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_MOVE,onEntityMove);
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_STOP,onEntityStop);
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_PLAYER_HP,onSetPlayerHP);
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_SKILL,onCreateSkill);
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_ENTITY_REMOVE,onEntityRemove);
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_GAME_OVER,onGameOver);
		}
		
		override protected function onDisposeBack():void
		{
			this._cadenceCtrl.destroyEvent();
			Game.EngineMgr.destroyEvent(MacroEvent.MSG_FROM_SERVER_ACTION);
			Game.EngineMgr.destroyEvent(MacroEvent.MSG_FROM_SERVER_POSITION);
			Game.EngineMgr.destroyEvent(MacroEvent.MSG_FROM_SERVER_FACE);
			Game.EngineMgr.destroyEvent(MacroEvent.MSG_FROM_SERVER_PLAYER_HP);
			Game.EngineMgr.destroyEvent(MacroEvent.MSG_FROM_SERVER_SKILL);
			Game.EngineMgr.destroyEvent(MacroEvent.MSG_FROM_SERVER_ENTITY_REMOVE);
			Game.EngineMgr.destroyEvent(MacroEvent.MSG_FROM_SERVER_NPC);
			Game.ownPlayer.setPlayerHP(100);
			this._losWinSprite.visible = false;
			
		}
	}
}